package com.yinp.proapp.module.wanandroid.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.yinp.proapp.adapter.CommonAdapter;
import com.yinp.proapp.adapter.SingleViewHolder;
import com.yinp.proapp.base.fragment.PresenterWanAndroidFragment;
import com.yinp.proapp.databinding.FragmentWanProjectBinding;
import com.yinp.proapp.databinding.ItemWanProjectBinding;
import com.yinp.proapp.module.wanandroid.bean.WanProjectListBean;
import com.yinp.proapp.module.wanandroid.web.retrofit.WanData2;
import com.yinp.proapp.module.wanandroid.web.retrofit.WanObserver2;
import com.yinp.proapp.utils.AppUtils;
import com.yinp.proapp.utils.JumpWebUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author :yinpeng
 * date      :2021/8/27
 * package   :com.yinp.proapp.module.wanandroid.fragment
 * describe  :
 */
public class WanProjectFragment extends PresenterWanAndroidFragment<FragmentWanProjectBinding> {
    private List<WanProjectListBean.DatasBean> dataList = new ArrayList<>();
    private CommonAdapter<WanProjectListBean.DatasBean> adapter;
    private int page = 0;

    public static WanProjectFragment getInstance() {
        WanProjectFragment wanProjectFragment = new WanProjectFragment();
        Bundle bundle = new Bundle();
        wanProjectFragment.setArguments(bundle);
        return wanProjectFragment;
    }

    @Override
    protected void initViews(View view) {
        initRecycler();
        refresh();
        getProjectList(true);
    }

    private void initRecycler() {
        adapter = new CommonAdapter<WanProjectListBean.DatasBean>(mContext,dataList) {
            @Override
            protected SingleViewHolder setComViewHolder(View view, int viewType, ViewGroup parent) {
                ItemWanProjectBinding itemWanProjectBinding = ItemWanProjectBinding.inflate(mInflater, parent, false);
                return new SingleViewHolder(itemWanProjectBinding);
            }

            @Override
            public void onBindItem(SingleViewHolder holder, int position, WanProjectListBean.DatasBean item) {
                ItemWanProjectBinding binding = (ItemWanProjectBinding) holder.getBinding();
                binding.tvTitle.setText(AppUtils.getValue(item.getTitle()));
                binding.tvContent.setText(AppUtils.getValue(item.getDesc()));
                binding.tvAuthor.setText("作者：" + (TextUtils.isEmpty(item.getAuthor()) ? item.getShareUser() : item.getAuthor()));
                binding.tvDate.setText(item.getNiceShareDate());
            }
        };
        adapter.setOnItemClickListener((position, view) -> {
            if (!TextUtils.isEmpty(dataList.get(position).getLink())) {
                JumpWebUtils.startWebView(getContext(), dataList.get(position).getTitle(), dataList.get(position).getLink());
            } else {
                showToast("缺少链接地址");
            }
        });
        LinearLayoutManager llm = new LinearLayoutManager(getContext());
        llm.setOrientation(RecyclerView.VERTICAL);
        bd.baseRecycle.setLayoutManager(llm);
        bd.baseRecycle.setHasFixedSize(true);
        bd.baseRecycle.setAdapter(adapter);
    }

    private void refresh() {
        //为下来刷新添加事件
        bd.baseRefresh.setOnRefreshListener(refreshlayout -> {
            page = 0;
            getProjectList(false);
        });
        //为上拉加载添加事件
        bd.baseRefresh.setOnLoadMoreListener(refreshLayout -> {
            page++;
            getProjectList(false);
        });
    }

    /**
     * 获取导航列表
     */
    private void getProjectList(boolean isLoad) {
        if (isLoad) {
            showLoading("加载中...");
        }
        presenter.getProjectList(page, new WanObserver2<WanData2<WanProjectListBean>>() {
            @Override
            public void onSuccess(WanData2<WanProjectListBean> o) {
                if (o.getData() != null) {
                    List<WanProjectListBean.DatasBean> list = o.getData().getDatas();
                    if (list != null && list.size() > 0) {
                        int length = dataList.size();
                        if (page == 0) {
                            dataList.clear();
                            dataList.addAll(list);
                            adapter.notifyDataSetChanged();
                            bd.baseRefresh.finishRefresh(true);
                        } else {
                            dataList.addAll(list);
                            adapter.notifyItemRangeChanged(length - 1, dataList.size());
                            bd.baseRefresh.finishLoadMore(true);
                        }
                        bd.baseRefresh.setVisibility(View.VISIBLE);
                        bd.bottom.noLl.setVisibility(View.GONE);
                    } else {
                        if (!isLoad && page == 0) {
                            bd.baseRefresh.finishRefresh(false);
                        } else if (!isLoad && page > 0) {
                            bd.baseRefresh.finishLoadMore(false);
                        }
                    }
                } else {
                    if (!isLoad && page == 0) {
                        bd.baseRefresh.setVisibility(View.GONE);
                        bd.bottom.noLl.setVisibility(View.VISIBLE);
                    } else if (!isLoad && page > 0) {
                        bd.baseRefresh.finishLoadMore(false);
                    }
                }
                if (isLoad) {
                    hideLoading();
                }
            }

            @Override
            public void onCodeFail(String msg, int code) {
                if (isLoad) {
                    hideLoading();
                }
                showToast(msg);
            }
        });
    }

}
